<?php
class Product extends AppModel{
    public $useTable  = 'product';
    public $lang_code = 'vn';
    
    public function get_product_by_type($type = null, $in_array_cate = array(), $start = null, $limit = null)
    {
        if($type == null)
            return array();
        
        
        $sql_in_array = "";
        if(!empty($in_array_cate))
        {
            $in_array = '';
            foreach($in_array_cate as $value)
            {
                if($in_array == '')
                    $in_array .= $value;
                else
                    $in_array .= ', '.$value;
            }
            
            $sql_in_array = " AND product.product_id IN (SELECT product_id
                            FROM product_category WHERE category_id IN ($in_array)) ";
        }
            
        $lang_code = $this->lang_code;
        $sql = "SELECT product.*, product_desc.*, link.*, image.*
                FROM product_desc, product
                LEFT JOIN link ON product.product_id = link.post_id AND link.lang_code = '$lang_code' AND link.type = 'product'
                LEFT JOIN image ON product.product_id = image.post_id AND image.type = 'product' AND image.`status` = 1
                WHERE product.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code' 
                AND product.`status` = 1 AND product.type = '$type' $sql_in_array
                ORDER BY product.sort_order ASC, product.product_id DESC
                LIMIT $start,$limit
                ";
        return $this->query($sql);
    }
    
    public function get_product_favorite($type = null)
    {
        if($type == null)
            return array();
        $lang_code = $this->lang_code;
        $sql = "SELECT product.*, product_desc.*, link.*, image.*
                FROM product_desc, product
                LEFT JOIN link ON product.product_id = link.post_id AND link.lang_code = '$lang_code' AND link.type = 'product'
                LEFT JOIN image ON product.product_id = image.post_id AND image.type = 'product' AND image.`status` = 1
                WHERE product.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code' 
                AND product.`status` = 1 AND product.favorite = 1 AND product.type = '$type' 
                ORDER BY product.sort_order ASC, product.product_id DESC";
        
        return $this->query($sql);
    }
    
    public function get_number_page($type = null, $in_array_cate = array())
    {
        $sql_in_array = "";
        if(!empty($in_array_cate))
        {
            $in_array = '';
            foreach($in_array_cate as $value)
            {
                if($in_array == '')
                    $in_array .= $value;
                else
                    $in_array .= ', '.$value;
            }
            
            $sql_in_array = " AND product.product_id IN (SELECT product_id
                            FROM product_category WHERE category_id IN ($in_array)) ";
        }
        
        $sql = "SELECT COUNT(*) AS count_product
                FROM product
                WHERE product.`status` = 1 AND product.type = '$type' $sql_in_array";
        $count = $this->query($sql);
        if(!empty($count))
        {
            $count_product = $count[0][0]['count_product'];
            $limit = Configure::read('product_per_page');
            $number_page = ceil($count_product/$limit);
            $number_page = max(1,$number_page);
            return $number_page;
        }
        else
            return 1;
    }
    
    public function get_info_product($type = null, $product_id = null)
    {
        $sql_type = "";
        if($type != '')
        {
            $sql_type = " AND product.type = '$type'";
        }
        
        $lang_code = $this->lang_code;
        $sql = "SELECT product.*, product_desc.*, link.*, image.name, custom.category_id, custom.title, custom.link
                FROM product_desc, product
                LEFT JOIN link ON product.product_id = link.post_id AND link.lang_code = '$lang_code' AND link.type = 'product'
                LEFT JOIN image ON product.product_id = image.post_id AND image.type = 'product' AND image.`status` = 1
                LEFT JOIN 
				(
					SELECT product_category.*, category_desc.title , link.link
				    FROM category_desc, product_category
				    LEFT JOIN link ON link.post_id = product_category.category_id AND link.type = 'category' AND link.lang_code = '$lang_code'
				    WHERE product_category.category_id = category_desc.category_id
				    AND category_desc.lang_code = '$lang_code'
				) custom ON product.product_id = custom.product_id AND custom.main_category = 1
                WHERE product.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code' 
                AND product.`status` = 1 $sql_type
				AND product.product_id = '$product_id'";
        $data = $this->query($sql);
        
        return $data;
    }
    
    function get_compo_by_product_id($product_id = null, $group = 'cutkit')
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT composition.compo_id, composition.title , product_compo.*
                FROM composition 
                LEFT JOIN product_compo ON composition.compo_id = product_compo.compo_id AND composition.lang_code = product_compo.lang_code AND 
                product_compo.product_id = $product_id
                WHERE composition.`group` = '$group' AND composition.lang_code = '$lang_code' AND composition.`status` = 1
                ORDER BY sort_order";

        $data = $this->query($sql);
        return $data;
    }
    
    function update_rating($sql = "")
    {
        $this->query($sql);
    }
    
}